package com.itxiaoxiong.chat.socket;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DatabaseUtils {

    private static final String url = "jdbc:mysql://localhost:3306/chat";
    private static final String user = "root";
    private static final String password = "241228";
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static List<Integer> getGroupMembers(int groupId) {
        List<Integer> members = new ArrayList<>();
        String query = "select user_id from group_members where group_id = " + groupId;

        try (Connection connection = DriverManager.getConnection(url, user, password);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(query)) {

            while (resultSet.next()) {
                members.add(resultSet.getInt("user_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return members;
    }
}
